<?php
define('SP_Version','0.5.8');

@include(APPBASE.'Config/lang.php');

require(SYSBASE.'Simple/Common.php');
require(SYSBASE.'Simple/l10n.php');
require(SYSBASE.'Simple/Controller.php');
require(SYSBASE.'Simple/Model.php');

if (floor(PHP_VERSION) < 5){
    define('PHPVer',4);
    require(SYSBASE.'Simple/Base/php4.php');
    require(SYSBASE.'Simple/Loader/Loader.php4.php');
}else{
    define('PHPVer',5);
    require(SYSBASE.'Simple/Base/php5.php');
    require(SYSBASE.'Simple/Loader/Loader.php5.php');
}

_bindtextdomain('system', SYSBASE.'Locale');
_bind_textdomain_codeset('system', 'UTF-8');
_bindtextdomain('message', isset($LANG['locale_path'])?$LANG['locale_path']:APPBASE.'Lang');
_bind_textdomain_codeset('message', 'UTF-8');
_textdomain('message');

$Gconfig =& Loader::config();

if(is_array($Gconfig['helpers'])){
    foreach($Gconfig['helpers'] as $helper){
        Loader::helper($helper);
    }
}

$Uri =& Loader::lib('uri');

$controller = $Uri->getClass();
$action     = $Uri->getMethod();

$controllerPath = APPBASE.'Controllers/'.$controller.'.php';

$controllerName = $controller.'Ctrl';
$actionName = $action.'Act';

if(!file_exists($controllerPath)){
    showError(__T("Unable to load your default controller.  Please make sure the controller specified in your Routes.php file is valid."));
}

require($controllerPath);

if(class_exists($controllerName)){

    $SP = new $controllerName;
        
    if (!method_exists($SP, $actionName)){
        show404(sprintf(__T("The function %sCtrl::%sAct() doesn't exist!"),$controller,$action));
    }

    require_once(APPBASE.'Plugin/Hook.php');
    $hook = new Hook();

    if(isset($Gconfig['hook']) && $Gconfig['hook'] == true){
            $hook->pre_controller($SP);
    }

    $SP->pre_controller();
    call_user_func_array(array(&$SP, $actionName),array_slice($Uri->getRsegments(), 2));
    $SP->post_controller();

    if(isset($Gconfig['hook']) && $Gconfig['hook'] == true){
            $hook->post_controller($SP);
    }
}else{
    show404(sprintf(__T("Controller %s Not Found!"),$controller));
}

$db =& Loader::database('db');
$db->close();